package com.hhxy.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class ReflectTest {
    public static void main(String[] args) {
/*        ArrayList<String> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        System.out.println(list1.getClass());//class java.util.ArrayList
        System.out.println(list2.getClass());//class java.util.ArrayList
        System.out.println(list1.getClass()==list2.getClass());//true*/
        ArrayList<Integer> list = new ArrayList<>();
        list.add(100);
//        list.add("abc"); // 报错
        Class listClass = list.getClass();//获取类Arrary类的类对象
        try {
            Method add = listClass.getDeclaredMethod("add",Object.class);//获取类对象中的成员方法对象
            add.invoke(list,"abc");
            System.out.println(list);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

}
